﻿/////////////////////////////////////////////////////////////////////////////////////////////
// <copyright file="OnPremiseSession.cs" company="Microsoft Corporation">
//     Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
/////////////////////////////////////////////////////////////////////////////////////////////

namespace Microsoft.LiveAtEdu.Samples.Powershell.OnPremise
{
    using System;
    using System.Management.Automation;
    using System.Management.Automation.Runspaces;
    using System.Security;

    /// <summary>
    /// Powershell commands to create, import, exit session for OnPremise scenario.
    /// </summary>
    internal class OnPremiseSession
    {
        /// <summary>
        /// Session runspace instance.
        /// </summary>
        private readonly Runspace runspace;

        /// <summary>
        /// Initializes a new instance of the OnPremiseSession class.
        /// </summary>
        /// <param name="runspace">Session runspace instance.</param>
        public OnPremiseSession(Runspace runspace)
        {
            if (runspace == null)
            {
                throw new ArgumentOutOfRangeException("runspace");
            }

            this.runspace = runspace;
        }

        #region Methods

        /// <summary>
        /// Create on-premise session
        /// </summary>
        /// <param name="userName">The user name to authenticate the session.</param>
        /// <param name="password">The password to authenticate the session.</param>
        /// <param name="connectionUri">Uri for creating the connection.</param>
        /// <returns>Powershell Session instance.</returns>
        public PSSession CreateSession(string userName, SecureString password, string connectionUri)
        {
            if (string.IsNullOrEmpty(userName))
            {
                throw new ArgumentOutOfRangeException("userName");
            }

            if (password == null)
            {
                throw new ArgumentOutOfRangeException("password");
            }

            if (string.IsNullOrEmpty(connectionUri))
            {
                throw new ArgumentOutOfRangeException("connectionUri");
            }

            this.runspace.SetCredential(userName, password);

            var command = new PSCommand();
            command.AddScript(string.Format(SessionScripts.NewSessionScript, connectionUri));
            return this.runspace.ExecuteCommand<PSSession>(command)[0];
        }

        /// <summary>
        /// Import Powershell Session.
        /// </summary>
        /// <param name="session">Powershell Session.</param>
        public void ImportSession(PSSession session)
        {
            if (session == null)
            {
                throw new ArgumentOutOfRangeException("session");
            }

            var command = new PSCommand();
            command.AddCommand(SessionScripts.ImportSessionScript);
            command.AddParameter(Constants.Session, session);
            this.runspace.ExecuteCommand(command);
        }

        /// <summary>
        /// Exit Powershell Session.
        /// </summary>
        /// <param name="session">Powershell Session.</param>
        public void ExitSession(PSSession session)
        {
            if (session == null)
            {
                throw new ArgumentOutOfRangeException("session");
            }

            var command = new PSCommand();
            command.AddCommand(SessionScripts.ExitSessionScript);
            command.AddParameter(Constants.Session, session);
            this.runspace.ExecuteCommand(command);
        }

        #endregion
    }
}
